package rexsee.core.menu;

import java.util.ArrayList;

import rexsee.core.browser.Browser;
import rexsee.core.style.StyleSheet;
import rexsee.core.widget.DivDialog;
import rexsee.core.widget.Tab;
import rexsee.core.widget.TabBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Html;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;

public class PopMenu {

	public static abstract class DynamicLocation {
		public abstract int[] run();
	}

	public final String id;
	public final StyleSheet styleSheet;
	public final ArrayList<PopMenuItem> items;

	public Runnable cancelRunnable = null;
	public Runnable dismissRunnable = null;
	public DynamicLocation dynamicLocation = null;

	private final Browser mBrowser;

	public PopMenu(Browser browser, String menuId, String style) {
		id = menuId;
		mBrowser = browser;
		styleSheet = (new StyleSheet(browser.getContext())).parseStyle(style);
		items = new ArrayList<PopMenuItem>();
	}

	public void add(PopMenuItem item) {
		if (item == null) return;
		items.add(item);
	}
	public PopMenuItem get(String id) {
		for (int i = 0; i < items.size(); i++) {
			PopMenuItem item = items.get(i);
			if (id.equals(item.id)) return item;
		}
		return null;
	}
	public void remove(String id) {
		for (int i = 0; i < items.size(); i++) {
			PopMenuItem item = items.get(i);
			if (id.equals(item.id)) items.remove(i);
		}
	}
	public void run(final Browser browser, String id) {
		PopMenuItem item = get(id);
		if (item != null && item.upRunnable != null) item.upRunnable.run();
	}
	public void open(final Browser browser) {
		final Context context = browser.getContext();
		final PopMenu mThis = this;
		((Activity) context).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (styleSheet.window_type.equalsIgnoreCase("context")) {
					final AlertDialog dialog = new AlertDialog.Builder(context).create();
					dialog.setInverseBackgroundForced(true);
					dialog.setIcon(browser.application.icon);
					dialog.setTitle(Html.fromHtml(styleSheet.label));
					String contextStyle = "white-space:normal;font-size:18px;border-width:0 0 1 0;border-bottom-color:#888888+#FFFFFF/2;height:wrapcontent;color:#000000;icon-visibility:hidden;padding:15px;text-align:left;";
					TabBar bar = new TabBar(context, mThis, "orientation:vertical;background-color:#FFFFFF;border-width:0;overflow-y:visible;scrollbar-y:visible;" + mThis.styleSheet.styleText);
					bar.setMinimumWidth(320);
					for (int i = 0; i < bar.mContainer.getChildCount(); i++) {
						final Tab tab = (Tab) bar.mContainer.getChildAt(i);
						tab.extraRunnable = new Runnable() {
							@Override
							public void run() {
								dialog.dismiss();
							}
						};
						final PopMenuItem item = items.get(i);
						tab.styleNormal.parseStyle(contextStyle + "background-color:#FFFFFF;").parseStyle(item.styleNormal.styleText);
						tab.stylePressed.parseStyle(contextStyle + "background-color:#DD8500+#FF9900+#FF9900+#FF9900+#DD8500;").parseStyle(item.stylePressed.styleText);
						tab.setSelected(false);
					}
					dialog.setCancelable(styleSheet.window_cancelable.equalsIgnoreCase("true"));
					dialog.setCanceledOnTouchOutside(styleSheet.window_outside_cancelable.equalsIgnoreCase("true"));
					dialog.setOnKeyListener(new Dialog.OnKeyListener() {
						@Override
						public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
							if (arg1 == KeyEvent.KEYCODE_SEARCH) return true;
							else return false;
						}
					});
					dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
						@Override
						public void onCancel(DialogInterface dialog) {
							try {
								cancelRunnable.run();
							} catch (Exception e) {
							}
						}
					});
					dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
						@Override
						public void onDismiss(DialogInterface dialog) {
							try {
								dismissRunnable.run();
							} catch (Exception e) {
							}
						}
					});
					dialog.setView(bar);
					dialog.show();
				} else {
					final DivDialog dialog;
					dialog = new DivDialog(browser, null, styleSheet.styleText);
					dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
						@Override
						public void onCancel(DialogInterface dialog) {
							try {
								cancelRunnable.run();
							} catch (Exception e) {
							}
						}
					});
					dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
						@Override
						public void onDismiss(DialogInterface dialog) {
							try {
								dismissRunnable.run();
							} catch (Exception e) {
							}
						}
					});
					if (styleSheet.window_cancelable.equalsIgnoreCase("true")) {
						dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
							@Override
							public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
								if (keyCode == KeyEvent.KEYCODE_BACK) {
									if (mBrowser.menu.isOptionsMenuOpen) {
										((Activity) browser.getContext()).closeOptionsMenu();
									} else {
										dialog.dismiss();
									}
									return true;
								} else {
									return false;
								}
							}
						});
					}
					TabBar bar = new TabBar(context, mThis);
					for (int i = 0; i < bar.mContainer.getChildCount(); i++) {
						final Tab tab = (Tab) bar.mContainer.getChildAt(i);
						tab.extraRunnable = new Runnable() {
							@Override
							public void run() {
								dialog.dismiss();
							}
						};
						tab.setSelected(false);
					}
					dialog.addChild(bar);
					dialog.start();
					if (dynamicLocation != null) {
						Window window = dialog.getWindow();
						WindowManager.LayoutParams attr = window.getAttributes();
						attr.gravity = Gravity.TOP | Gravity.LEFT;
						int[] location = dynamicLocation.run();
						if (location[0] >= 0) attr.x = location[0];
						if (location[1] >= 0) attr.y = location[1];
						window.setAttributes(attr);
					}
				}
			}
		});

	}

	public void composeOptionsMenu(Menu menu) {
		menu.clear();
		for (int i = 0; i < items.size(); i++) {
			PopMenuItem item = items.get(i);
			menu.add(0, i, 0, item.styleNormal.label).setIcon(item.styleNormal.icon);
		}
	}

	public void destroy() {
		for (int i = 0; i < items.size(); i++) {
			try {
				items.get(i).destroy();
			} catch (Exception e) {
			}
		}
		styleSheet.destroy();
	}

}
